<?php
/**
 * 分享
 * Created by PhpStorm.
 * User: Earl <guo@seosite.cn>
 * Date: 2018年04月16日
 * Time: 10:16:44
 * Updatetime:
 * updater：
 */
namespace App\Controller;
header("Access-Control-Allow-Origin: *");
use Think\Controller;

class H5Controller extends Controller {

    /**
     * @desc 通用分享
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function generalShare($param)
    {
        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('type');

        unset($check);

        $type = $param['type'];

        $id = $param['id'];


        if ($type==1){//视频直播分享

            $info = M('direct')->field('id,uid,title,headsmall,meeting_help')->where(['id'=>$id])->find();

            $title = $info['title'];
            $content = $info['meeting_help'];
            $headsmall = $info['headsmall'];
            $url = C('site_url').'website/blogger/column/index.html#/detail/'.$info['uid'];

        }elseif ($type==2){//音频直播分享

            $info = M('audio')->field('id,uid,title,headsmall,meeting_help')->where(['id'=>$id])->find();

            $title = $info['title'];
            $content = $info['meeting_help'];
            $headsmall = $info['headsmall'];
            $url = C('site_url').'website/blogger/column/index.html#/detail/'.$info['uid'];

        }elseif ($type==3){//视频分享

            $info = M('video')->field('id,title,headsmall,abstract')->where(['id'=>$id])->find();

            $title = $info['title'];
            $content = $info['abstract'];
            $headsmall = $info['headsmall'];
            $url = C('site_url').'website/blogger/video/index.html#/video/'.$info['id'];

        }elseif ($type==4){//音频分享

            $info = M('mp3')->field('id,title,headsmall,abstract')->where(['id'=>$id])->find();

            $title = $info['title'];
            $content = $info['abstract'];
            $headsmall = $info['headsmall'];

            $url = C('site_url').'website/blogger/audio/index.html#/audio/'.$info['id'];
        }elseif($type==5){//未开播分享

            $info = M('User')->where(['id'=>$param['id']])->find();
            $title = $info['nickname'].'的直播间';
            $content = $info[''];
            $headsmall = $info['headsmall'];
            $url = C('site_url').'website/blogger/column/index.html#/detail/'.$info['uid'];
        }

        $retData = array(
            'title'     =>  $title,
            'content'   =>  $content,
            'headsmall' =>  $headsmall,
            'linkUrl'   =>  $this->short($url),
        );

        return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,$retData);
    }

    /**
     * @desc 生成短连接
     * @author Egor<http://gitee.com/seosite>
     * @param $url
     * @return array
     */
    public function short($url) {

        //http://api.t.sina.com.cn/short_url/shorten.json?source=579177356&url_long=http://www.seosite.cn

        $center = 'http://api.t.sina.com.cn/short_url/shorten.json';
        $source = '579177356';
        $get_url = $center.'?source='.$source.'&url_long='.$url;
        $content = curlGet($get_url);

        $j_content =   json_decode($content);

        if (isset($j_content->error_code)){
            return showErrorMsg($j_content->error,10002);
        }

        return $j_content[0]->url_short;
    }
}